var bsurl = require('utils/bsurl.js')
var nt = require('utils/nt.js')
var util = require('utils/util.js')
var api = require('utils/api.js')
var player = wx.getBackgroundAudioManager()
App({
  onLaunch: function () {
    var that = this;
    //播放错误
    player.onError(() => {
      wx.showToast({
        title: '音频加载失败',
        icon: 'none'
      })
    })
    //自然播放完成
    player.onEnded(() => {
      that.toggleMusic(null, 1, 'auto')
    })
    //停止播放
    player.onStop(() => {
      console.log('stop')
    })
    //监听音乐暂停，保存播放进度广播暂停状态
    player.onPause(() => {
      that.globalData.position = player.currentTime
      that.globalData.page.setData({
        playing: false
      })
    })
    player.onPlay(() => {
      wx.hideLoading()
      that.globalData.page.setData({
        playing: true,
        listIndex: that.globalData.index,
        music: that.globalData.music,
      })
      that.addPopularity()
    })
    player.onWaiting(() => {
      wx.showLoading({
        title: '加载中...',
        mask: true,
      })
    })
    player.onCanplay(() => {
      wx.hideLoading()
    })
    player.onPrev(() =>{
      that.toggleMusic(() => { }, -1, 'onPrev')
    })
    player.onNext(() =>{
      that.toggleMusic(() => { }, 1, 'onNext')
    })
    that.globalData.systemInfo = wx.getSystemInfoSync()
  },
  onShow: function () {
  },
  onHide: function () {
  },
  globalData: {
    index: -1,
    playId: 0,
    shuffle: 1,
    page: null,
    music: null,
    playing: false,
    idx: -1,
  },
  registerPage: function (page) {
    var that = this
    that.globalData.page = page
  },
  listDelOne: function (cb, index) {
    var that = this
    var tmpList = that.globalData.list
    var delMusic = tmpList[index]
    tmpList.splice(index, 1)
    that.globalData.list = tmpList
    if (that.globalData.music.id == delMusic.id && index < tmpList.length - 1) {
      that.play(() => { }, index)
    }
    cb && cb(that.globalData.list)
  },
  listDelAll: function (cb) {
    var that = this
    that.globalData.list = []
    cb && cb(that.globalData.list)
  },
  toggleMusic: function (cb, shift, trigger) {
    var that = this
    if (that.globalData.page == null) {
      console.log('First register page')
      return false
    }
    if (trigger != 'list') {
      var newIndex = 0
      if (that.globalData.shuffle == 1 || that.globalData.shuffle == 2) {
        if (trigger != 'auto'){
          newIndex = parseInt(that.globalData.index) + parseInt(shift)
          if (newIndex > that.globalData.list.length - 1) {
            newIndex = 0
          }
        }else{
          if (that.globalData.shuffle == 1){
            newIndex = parseInt(that.globalData.index) + parseInt(shift)
            if (newIndex > that.globalData.list.length - 1) {
              newIndex = 0
            }
          }else{
            newIndex = that.globalData.index
          }
        }
      } else if (that.globalData.shuffle == 3) {
        for (; ;) {
          newIndex = util.random(0, that.globalData.list.length)
          if (newIndex < that.globalData.list.length && newIndex != that.globalData.index) {
            break;
          }
        }
      }
      if (newIndex < 0) {
        wx.showToast({
          title: '已经第一首',
          duration: 2000
        })
        return false
      } else if (newIndex > that.globalData.list.length - 1) {
        wx.showToast({
          title: '已经最后一首',
          duration: 2000
        })
        return false
      } else {
        console.log(newIndex)
        that.play(() => { }, newIndex)
      }
    } else {
      that.play(() => { }, shift)
    }
    cb && cb()
  },
  playMode: function (shuffle) {
    var that = this
    if (that.globalData.page == null) {
      console.log('First register page')
      return false
    }
    that.globalData.shuffle = shuffle
    console.log(that.globalData.shuffle)
  },
  play: function (cb, index) {
    var that = this
    if (that.globalData.page == null) {
      console.log('First register page')
      return false
    }
    var music = that.globalData.list[index]
    that.globalData.index = index
    if (that.globalData.music == null) {
      that.globalData.music = music
      player.title = music.title
      player.src = music.play_path_64
    } else {
      if (music.id != that.globalData.music.id || that.globalData.shuffle == 2) {
        that.globalData.music = music
        player.title = music.title
        player.src = music.play_path_64
      }
    }
    cb && cb()
  },
  pause: function (cb) {
    var that = this
    player.pause()
    cb && cb()
  },
  stop: function (cb) {
    var that = this
    player.stop()
    cb && cb()
  },
  seek: function (cb, postion) {
    var that = this
    if (postion == -1) {
      player.startTime = that.globalData.position
      player.play()
    } else if (postion == -2) {
      player.seek(0)
      player.play()
    }else {
      player.seek(postion)
    }
    cb && cb()
  },
  getPlayInfo: function () {
    var paused = player.paused
    if (player.paused == 0) {
      paused = false
    } else if (player.paused == 1) {
      paused = true
    }
    if (paused == false) {
      wx.hideLoading()
    }
    return {
      duration: player.duration,
      currentTime: parseInt(player.currentTime),
      paused: paused,
      buffered: parseInt(player.buffered),
    }
  },
  addPopularity: function(){
    var that = this
    wx.request({
      url: api.addCount + "?album_id=" + that.globalData.music.album_id,
      success: function (res) {
      }
    })
  }
})

